﻿function AnalyseBar(container) {
    this.absoluteAvgs = [];
    this.relativeAvgs = [];
    this.series = [];
    this.container = container;
    this.xAxisCategories = [];
}

AnalyseBar.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            renderTo: _.container,
            height: 300,
            type: 'bar'
        },
        lang: {
            contextButtonTitle: '导出图片',
            loading: '加载中',
            noData: '没有数据'
        },
        title: {
            text: '模型准确度柱状图'
        },
        credits: {
            enabled: false
        },
        xAxis: {
            categories:  this.xAxisCategories,//['多元回归', '同期回归', '神经网络', '聚类回归'],
            title: {
                text: null
            }
        },
        yAxis: {
            title: {
                text: null
            },
            labels: {
                style: {
                    fontSize: '10px'
                }
            }
        },
        tooltip: {
            shared: true
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },

        credits: {
            enabled: false
        },
        series: _.series
    }
};

AnalyseBar.prototype.onChartLoad = function () {
};

AnalyseBar.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

AnalyseBar.prototype.initData = function (absolutedata, relativedata) {
    var _ = this;
    var absoluteValueInForecastModel0 = 0;
    var absoluteValueInForecastModel0Count = 0;
    var absoluteValueInForecastModel1 = 0;
    var absoluteValueInForecastModel1Count = 0;
    var absoluteValueInForecastModel2 = 0;
    var absoluteValueInForecastModel2Count = 0;
    var absoluteValueInForecastModel3 = 0;
    var absoluteValueInForecastModel3Count = 0;
    var absoluteValueInForecastModel5 = 0;
    var absoluteValueInForecastModel5Count = 0;
    var relativeValueInInForecastModel0 = 0;
    var relativeValueInInForecastModel0Count = 0;
    var relativeValueInInForecastModel1 = 0;
    var relativeValueInInForecastModel1Count = 0;
    var relativeValueInInForecastModel2 = 0;
    var relativeValueInInForecastModel2Count = 0;
    var relativeValueInInForecastModel3 = 0;
    var relativeValueInInForecastModel3Count = 0;
    var relativeValueInInForecastModel5 = 0;
    var relativeValueInInForecastModel5Count = 0;
    if (absolutedata["0"] != undefined) {
        _.xAxisCategories.push("多模式择优");

        for (var i = 0; i < absolutedata["0"].data.length; i++) {
            absoluteValueInForecastModel0 += absolutedata["0"].data[i];
            absoluteValueInForecastModel0Count++;
        }
        _.absoluteAvgs.push(parseFloat((absoluteValueInForecastModel0 / absoluteValueInForecastModel0Count).toFixed(3)));
    }
    if (absolutedata["1"] != undefined) {
        _.xAxisCategories.push("多元回归");

        for (var i = 0; i < absolutedata["1"].data.length; i++) {
            absoluteValueInForecastModel1 += absolutedata["1"].data[i];
            absoluteValueInForecastModel1Count++;
        }
        _.absoluteAvgs.push(parseFloat((absoluteValueInForecastModel1 / absoluteValueInForecastModel1Count).toFixed(3)));
    }
    if (absolutedata["2"] != undefined) {
        _.xAxisCategories.push("同期回归");
        for (var i = 0; i < absolutedata["2"].data.length; i++) {
            absoluteValueInForecastModel2 += absolutedata["2"].data[i];
            absoluteValueInForecastModel2Count++;
        }
        _.absoluteAvgs.push(parseFloat((absoluteValueInForecastModel2 / absoluteValueInForecastModel2Count).toFixed(3)));
    }
    if (absolutedata["3"] != undefined) {
        _.xAxisCategories.push("神经网络");
        for (var i = 0; i < absolutedata["3"].data.length; i++) {
            absoluteValueInForecastModel3 += absolutedata["3"].data[i];
            absoluteValueInForecastModel3Count++;
        }
        _.absoluteAvgs.push(parseFloat((absoluteValueInForecastModel3 / absoluteValueInForecastModel3Count).toFixed(3)));
    }
    if (absolutedata["5"] != undefined) {
        _.xAxisCategories.push("聚类回归");
        for (var i = 0; i < absolutedata["5"].data.length; i++) {
            absoluteValueInForecastModel5 += absolutedata["5"].data[i];
            absoluteValueInForecastModel5Count++;
        }
        _.absoluteAvgs.push(parseFloat((absoluteValueInForecastModel5 / absoluteValueInForecastModel5Count).toFixed(3)));
    }
    if (relativedata["0"] != undefined) {
        for (var i = 0; i < relativedata["0"].data.length; i++) {
            relativeValueInInForecastModel0 += relativedata["0"].data[i];
            relativeValueInInForecastModel0Count++;
        }
        _.relativeAvgs.push(parseFloat((relativeValueInInForecastModel0 / relativeValueInInForecastModel0Count).toFixed(3)));
    }
    if (relativedata["1"] != undefined) {
        for (var i = 0; i < relativedata["1"].data.length; i++) {
            relativeValueInInForecastModel1 += relativedata["1"].data[i];
            relativeValueInInForecastModel1Count++;
        }
        _.relativeAvgs.push(parseFloat((relativeValueInInForecastModel1 / relativeValueInInForecastModel1Count).toFixed(3)));
    }
    if (relativedata["2"] != undefined) {
        for (var i = 0; i < relativedata["2"].data.length; i++) {
            relativeValueInInForecastModel2 += relativedata["2"].data[i];
            relativeValueInInForecastModel2Count++;
        }
        _.relativeAvgs.push(parseFloat((relativeValueInInForecastModel2 / relativeValueInInForecastModel2Count).toFixed(3)));
    }
    if (relativedata["3"] != undefined) {
        for (var i = 0; i < relativedata["3"].data.length; i++) {
            relativeValueInInForecastModel3 += relativedata["3"].data[i];
            relativeValueInInForecastModel3Count++;
        }
        _.relativeAvgs.push(parseFloat((relativeValueInInForecastModel3 / relativeValueInInForecastModel3Count).toFixed(3)));
    }
    if (relativedata["5"] != undefined) {
        for (var i = 0; i < relativedata["5"].data.length; i++) {
            relativeValueInInForecastModel5 += relativedata["5"].data[i];
            relativeValueInInForecastModel5Count++;
        }
        _.relativeAvgs.push(parseFloat((relativeValueInInForecastModel5 / relativeValueInInForecastModel5Count).toFixed(3)));
    }
    //var _ = this;
    //if (jsonData.length > 0) {
    //    var d = jsonData;

    //    var absoluteValueInForecastModel0 = 0;
    //    var absoluteValueInForecastModel0Count = 0;
    //    var absoluteValueInForecastModel1 = 0;
    //    var absoluteValueInForecastModel1Count = 0;
    //    var absoluteValueInForecastModel2 = 0;
    //    var absoluteValueInForecastModel2Count = 0;
    //    var absoluteValueInForecastModel3 = 0;
    //    var absoluteValueInForecastModel3Count = 0;
    //    var absoluteValueInForecastModel5 = 0;
    //    var absoluteValueInForecastModel5Count = 0;

    //    var relativeValueInInForecastModel0 = 0;
    //    var relativeValueInInForecastModel0Count = 0;
    //    var relativeValueInInForecastModel1 = 0;
    //    var relativeValueInInForecastModel1Count = 0;
    //    var relativeValueInInForecastModel2 = 0;
    //    var relativeValueInInForecastModel2Count = 0;
    //    var relativeValueInInForecastModel3 = 0;
    //    var relativeValueInInForecastModel3Count = 0;
    //    var relativeValueInInForecastModel5 = 0;
    //    var relativeValueInInForecastModel5Count = 0;

    //    for (var i = 0; i < jsonData.length; i++) {
    //        if (d[i].AbsoluteValueInForecastModel0 != null) {
    //            absoluteValueInForecastModel0 += d[i].AbsoluteValueInForecastModel0;
    //            absoluteValueInForecastModel0Count++;
    //        }
    //        if (d[i].AbsoluteValueInForecastModel1 != null) {
    //            absoluteValueInForecastModel1 += d[i].AbsoluteValueInForecastModel1;
    //            absoluteValueInForecastModel1Count++;
    //        }
    //        if (d[i].AbsoluteValueInForecastModel2 != null) {
    //            absoluteValueInForecastModel2 += d[i].AbsoluteValueInForecastModel2;
    //            absoluteValueInForecastModel2Count++;
    //        }
    //        if (d[i].AbsoluteValueInForecastModel3 != null) {
    //            absoluteValueInForecastModel3 += d[i].AbsoluteValueInForecastModel3;
    //            absoluteValueInForecastModel3Count++;
    //        }
    //        if (d[i].AbsoluteValueInForecastModel5 != null) {
    //            absoluteValueInForecastModel5 += d[i].AbsoluteValueInForecastModel5;
    //            absoluteValueInForecastModel5Count++;
    //        }
    //        if (d[i].RelativeValueInInForecastModel0 != null) {
    //            relativeValueInInForecastModel0 += d[i].RelativeValueInInForecastModel0;
    //            relativeValueInInForecastModel0Count++;
    //        }
    //        if (d[i].RelativeValueInInForecastModel1 != null) {
    //            relativeValueInInForecastModel1 += d[i].RelativeValueInInForecastModel1;
    //            relativeValueInInForecastModel1Count++;
    //        }
    //        if (d[i].RelativeValueInInForecastModel2 != null) {
    //            relativeValueInInForecastModel2 += d[i].RelativeValueInInForecastModel2;
    //            relativeValueInInForecastModel2Count++;
    //        }
    //        if (d[i].RelativeValueInInForecastModel3 != null) {
    //            relativeValueInInForecastModel3 += d[i].RelativeValueInInForecastModel3;
    //            relativeValueInInForecastModel3Count++;
    //        }
    //        if (d[i].RelativeValueInInForecastModel5 != null) {
    //            relativeValueInInForecastModel5 += d[i].RelativeValueInInForecastModel5;
    //            relativeValueInInForecastModel5Count++;
    //        }
    //    }

        //_.absoluteAvgs.push(parseFloat((absoluteValueInForecastModel0 / absoluteValueInForecastModel0Count).toFixed(3)));
       
       
     
    

        // _.relativeAvgs.push(parseFloat((relativeValueInInForecastModel0 / absoluteValueInForecastModel0Count).toFixed(3)));
      
       
       
     

        _.series = _.getSeries(_.series);
        _.createChart();
    //}
    //else {
    //    return "no data";
    //}
};

AnalyseBar.prototype.getSeries = function (series) {
    var _ = this, series = [];
    series.push({
        name: "相对误差均值",
        color: '#d9534f',
        data: _.relativeAvgs,
        zIndex: 1

    });
    series.push({
        name: '绝对误差均值',
        color: '#f0ad4e',
        data: _.absoluteAvgs
    });

    return series;
}
